<template>
  <div class="app-container">
    <div class="filter-container" style="padding-bottom: 20px;text-align: right;">
      <el-button class="filter-item" style="margin-left: 10px;" v-if="hasPower('/role/add')" @click="$router.push('/role/add')" type="primary" icon="el-icon-edit">添加角色</el-button>
    </div>
    <el-table :data="data.list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="index" label="序号" width="50">
      </el-table-column>
      <el-table-column label="角色名" width="160">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="角色描述" min-width="200">
        <template slot-scope="scope">
          <span>{{scope.row.mark}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" v-if="hasPower('/admin/list')" @click="$router.push({path: '/admin/list', query: {role: scope.row._id}})">查看</el-button>
          <el-button type="primary" size="mini" @click="$router.push({path: '/role/edit', query: {_id: scope.row._id}})" v-if="hasPower('/role/edit')">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleRemove(scope.row)" v-if="hasPower('/role/remove')">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top:30px;"
      background
      @current-change="handleCurrentChange"
      :current-page="data.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="data.limit"
      layout="total, prev, pager, next"
      :total="data.total">
    </el-pagination>

  </div>
</template>

<script>
import { getList, remove } from '@/api/role'
import { getCount } from '@/api/admin'

export default {
  data() {
    return {
      data: {
        limit: 10,
        total: 0,
        page: 1
      },
      listQuery: {},
      listLoading: true
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    countAdmin(o, cb) {
      getCount({ role: o._id }).then(res => {
        cb(o, res.data.total)
      })
    },
    handleRemove(o, count) {
      if (typeof count === 'undefined') {
        // 统计该角色下管理员数量
        return this.countAdmin(o, this.handleRemove)
      }
      var msg = '确认继续删除角色?'
      if (count > 0) msg = '该角色下还有管理员，是否确认删除？'
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({
          _id: o._id
        }).then(res => {
          if (res.code !== 30000) {
            return this.$message({
              type: 'error',
              message: '请求失败'
            })
          }
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.data = response.data
        this.listLoading = false
      })
    }
  }
}
</script>
